module.exports = class extends think.Model {
	get tablePrefix() {
		return 'app_wxbot_';
	}
	findOne(id) {
		return this.where({
			id: id
		}).find()
	}

	setOne(id, data) {
		return this.where({
			id: id
		}).update(data);
	}
	
	
	async joinAssetTable(posts, where, fields) {
		let pageSize = parseInt(posts.length);
		let start = parseInt(posts.start);
		let page = start / pageSize + 1;
		let draw = parseInt(posts.draw);
		let data = {};
		let sort = {
			
		};
		if(!think.isEmpty(posts.level)) {
			where.level = ['like', '%' + posts.level + '%'];
		}
		if(!think.isEmpty(posts.userName)) {
			where.userName = ['like', '%' + posts.userName + '%'];

		}
		if(posts['order[0][dir]']) {
			sort[posts['columns[' + posts['order[0][column]'] + '][data]']] = posts['order[0][dir]'];
		}
		let count = await this.alias('m').where(where).join("sys_asset AS a ON m.user_id=a.id").count();

		if(count > 0) {

			let res = await this.alias('m').where(where).order(sort).page(page, pageSize).join("sys_asset AS a ON m.user_id=a.id").field(fields + ",a.score,a.amount,a.rebate,a.team_count,a.agency_id,a.level").select();

			data = {
				"draw": draw,
				"recordsTotal": pageSize,
				"recordsFiltered": count,
				"data": res
			}

		} else {
			data = {
				"draw": draw,
				"recordsTotal": pageSize,
				"recordsFiltered": 0,
				"data": []
			}
		}
		return data;

	}
	
	async dataTable(posts, where) {
		let pageSize = parseInt(posts.length);
		let start = parseInt(posts.start);
		let page = start / pageSize + 1;
		let draw = parseInt(posts.draw);
		let data = {};
		let sort = {};

		if(posts['order[0][dir]']) {
			sort[posts['columns[' + posts['order[0][column]'] + '][data]']] = posts['order[0][dir]'];
		}
		let count = await this.where(where).count();

		if(count > 0) {

			let res = await this.where(where).order(sort).page(page, pageSize).select();

			data = {
				"draw": draw,
				"recordsTotal": pageSize,
				"recordsFiltered": count,
				"data": res
			}

		} else {
			data = {
				"draw": draw,
				"recordsTotal": pageSize,
				"recordsFiltered": 0,
				"data": []
			}
		}
		return data;

	}
	
	
	
	
	
};